package com.captain.special.链表;

import com.captain.leetcode.链表.ListNode;

import java.util.List;

/**
 * Des:
 *
 * @author XL
 * @Date 2022/3/22 11:33
 */
public class 合并两个有序链表21 {


    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        //循环两个链表
        ListNode dummy = new ListNode(-1);
        ListNode cur = dummy;
        while (list1 != null && list2 != null) {
            int val1 = list1.val;
            int val2 = list2.val;
            if (val1 <= val2) {
                cur.next = new ListNode(val1);
                list1 = list1.next;
            } else {
                cur.next = new ListNode(val2);
                list2 = list2.next;
            }
            cur = cur.next;
        }

        if (list1 == null) {
            cur.next = list2;
        } else {
            cur.next = list1;
        }
        return dummy.next;
    }
}
